<?
  include "../common/db_conn.php";  

  $sd_idx = $_GET["sd_idx"];
  $sq_idx = $_GET["sq_idx"];
  
  if ($sd_idx == "") {  // wrong
    $sd_idx = 0;
  }
  if ($sq_idx == "") {   // new question
    $sq_idx = 0;
  }
  
  $query_d = "select sd_idx, sd_text from sv_dlist where sd_idx = $sd_idx";
  $result_d = mysql_query($query_d);
  $rows_d = mysql_fetch_array($result_d);
  
  if ($sq_idx > 0) {
    $query_q = "select sq_idx, sq_text, sq_type, sq_from, sq_to, sq_answer from sv_qlist where sq_idx = $sq_idx limit 1";
    $result_q = mysql_query($query_q);
    $num_rows_q = mysql_num_rows($result_q);
    $rows_q = mysql_fetch_array($result_q);
    
    $sq_text = $rows_q["sq_text"];
    $sq_type = $rows_q["sq_type"];
    $sq_answer = $rows_q["sq_answer"];
    $sq_from = $rows_q["sq_from"];
    $sq_to = $rows_q["sq_to"];

    $query_a = "select sa_idx, sq_idx, sa_text from sv_alist where sq_idx = $sq_idx order by sa_idx asc";
    $result_a = mysql_query($query_a);
    $num_rows_a = mysql_num_rows($result_a);
    
    echo("num_rows_a[$num_rows_a]<br>");
    echo("sq_type[$sq_type] sq_answer[$sq_answer] sq_from[$sq_from] sq_to[$sq_to]<br>");
  }
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
  div.qt {
    display: none;
  }
</style>
<script>
  function showType()
  {
    for (i = 0; i < document.frm.sq_type.length; i++)
    {
      if (document.frm.sq_type[i].checked) {
        document.getElementById("qtype_"+i).style.display = "block";
      } else {
        document.getElementById("qtype_"+i).style.display = "none";
      }
    }
  }
  
  function showTest() 
  {
    var display;
    
    if (document.frm.a_test[0].checked == true)
      display = "block";
    else
      display = "none";
      
    console.log(document.frm.a_test[0].checked);
    for (i = 0; i < document.frm.sq_answer.length; i++)
      document.frm.sq_answer[i].style.display = display;
      
    if (document.frm.a_test[2].checked == true) {
      display = "block";
    } else {
      display = "none";
    }
    document.getElementById("ans_cnt").style.display = display;

  }
  
  function showAnswers()
  {
    var count = document.frm.count.value;
    for (i = 0; i < 10; i++) {
      if (i < count)
        document.getElementById("test_"+i).style.display = "block";
      else
        document.getElementById("test_"+i).style.display = "none";
    } 
  }
  
  function preview(option) 
  {
    var  text = document.frm.sq_text.value;
    if (option == 3) {
      div = document.getElementById("qtype_3_preview");
      text = text.replace("${number}", "<input type=\"text\" name=\"tttt\" size=\"3\" disabled>");
    } else if (option == 4) {
      div = document.getElementById("qtype_4_preview");
      text = text.replace("${year}", "<input type=\"text\" name=\"tttt\" size=\"2\" disabled>");
      text = text.replace("${month}", "<input type=\"text\" name=\"tttt\" size=\"2\" disabled>");
    }
    
    div.innerHTML = text;
  }
</script>
</head>
<body>

<h1>관리자 페이지</h1>
<h2><?=$rows_d["sd_idx"]?>. <?=stripslashes($rows_d["sd_text"])?></h2>
<button onClick="location.href='./q_list.html?sd_idx=<?=$sd_idx?>'">목록으로</button>

<!---
문제idx
답변no

-->
<form name="frm" method="post" target="ifr" action="q_proc.php">
<input type="hidden" name="ptype" value="2">
<input type="hidden" name="sq_idx" value="<?=$sq_idx?>">
<input type="hidden" name="sd_idx" value="<?=$sd_idx?>">
<h2>문제 입력</h2>
<textarea name="sq_text" cols="80" rows="3"><?=stripslashes($rows_q["sq_text"])?></textarea><br>
선다형<input type="radio" name="sq_type" value="0" onClick="showType()"> /
점수형<input type="radio" name="sq_type" value="1" onClick="showType()"> /
선택형<input type="radio" name="sq_type" value="2" onClick="showType()"> /
주관식<input type="radio" name="sq_type" value="3" onClick="showType()"> /
년월형<input type="radio" name="sq_type" value="4" onClick="showType()">

<div id="qtype_0" class="qt">
개수: 
<select name="count" onChange="showAnswers()">
<?
  for ($i = 1; $i < 10; $i++)
  {
    if ($num_rows_a == $i+1)
      $selected =   " selected";
    else
      $selected = "";
?>
  <option value="<?=$i+1?>"<?=$selected?>><?=$i+1?></option>
<?
  }
?>
</select> (복수응답형의 경우 기타는 제외한 개수)
<span id="ans_cnt">
응답 가능 수 (기타를 포함): 
<input type="text" name="sq_from" maxlength="2" size="2" value="<?=$sq_from?>"> 
~ 
<input type="text" name="sq_to" maxlength="2" size="2" value="<?=$sq_to?>">
</span>
<br>
<input type="radio" name="a_test" value="0" onClick="showTest()">정답존재
<?
  if ($sq_idx > 0 && $sq_type == 0 && $sq_answer == 0 && $sq_from == "") {
    $checked = " checked";
  } else {
    $checked = "";
  }
?>
<input type="radio" name="a_test" value="1" onClick="showTest()"<?=$checked?>>단순설문
<?
  if ($sq_idx > 0 && $sq_type == 0 && $sq_answer == 0 && $sq_from != "") {
    $checked = " checked";
  } else {
    $checked = "";
  }
?>
<input type="radio" name="a_test" value="2" onClick="showTest()"<?=$checked?>>복수응답형(+기타)<br>
<table>

<?
  for ($i = 0; $i < 10; $i++) 
  {
    if ($sq_idx > 0) {
      $rows_a = mysql_fetch_array($result_a);
    } else {
      $sq_answer = 1;
    }   
    
    if ($sq_answer == $i+1)
      $checked = " checked";
    else
      $checked = "";
?>
<tr id="test_<?=$i?>">
<td><?=$i+1?>번 답변:</td>
<td><input type="radio" name="sq_answer" value="<?=($i+1)?>"<?=$checked?>></td>
<td><input type="text" name="a<?=$i+1?>" value="<?=$rows_a["sa_text"]?>"></td>
</tr>
<?
  }
?>
</table>
</div>
<script>
  showAnswers();
</script>
<div id="qtype_1" class="qt">
<? 
  for ($i = 0; $i < 11; $i++)
  {
?>
<input type="radio" name="qtype_1" value="<?=($i+1)?>" disabled><?=($i*10)?>점 
<?
    if (($i+1) % 3 == 0) {
?>
    <br>
<?
    }
  }
?>
</div>

<div id="qtype_2" class="qt">
<input type="radio" name="qtype_2" value="1" disabled>항상 그렇다<br>
<input type="radio" name="qtype_2" value="1" disabled>보통 그렇다<br>
<input type="radio" name="qtype_2" value="1" disabled>그렇지 않다<br>
</div>

<div id="qtype_3" class="qt">
<input type="button" onClick="preview(3)" value="미리보기">
<p>
  주관식 입력 예시) ... 개수는 몇 개일까요? <b>${number}</b>개<br>
  단, 반드시 ${number}가 한 번 나와야 합니다.
</p>
<div id="qtype_3_preview"></div>
</div>

<div id="qtype_4" class="qt">
<input type="button" onClick="preview(4)" value="미리보기">
<p>
  년월형 입력 예시) ... 한 기간은? <b>${year}</b>년 <b>${month}</b>개월<br>
  단, 반드시 ${year}와 ${month}가 각각 한 번씩 나와야 합니다.
</p>
<div id="qtype_4_preview"></div>
</div>

<input type="submit" value="저장">
</form>
<?
  if ($sq_idx > 0) {
?>
<script>
  document.frm.sq_type[<?=$sq_type?>].checked = true;
  showType();
  
</script>
<?    
  }
  if ($sq_idx > 0 && $sq_type == 0 && $sq_answer == 0) {
  
    if ($sq_from == "") {
      $a_test_no = 1;
    } else {
      $a_test_no = 2;
    }
?>    
<script>
  document.frm.a_test[<?=$a_test_no?>].checked = true;
</script> 
<?
  } else {
?>
<script>
  document.frm.a_test[0].checked = true;
</script> 
<? 
  }
?>
<br>
<iframe src="about:blank" name="ifr" width="300" height="300"></iframe>
<script>
  showTest();
</script>
</body>
</html>
<?
  include "../common/db_close.php";
?>
